<?php
namespace api\mm\controller;

use cmf\controller\RestBaseController;
use api\mm\model\AppStatisticModel;
use api\mm\service\HostReqest;

class IndexController extends RestBaseController{
    public function info(){
        $requst = new HostReqest();
        $data = $requst->studentList();
        if($data['status']==200){
            $this->success($data['message'],$data['data']);
        }
        $this->error('err');
    }
    public function getStudentStatus(){
        $studentId = $this->request->get('studentid');
        $requst = new HostReqest();
        $data = $requst->getStudentStatus($studentId);
        if($data['status']==200){
            $this->success($data['message'],$data['data']);
        }
        $this->error('err');
    }

    public function statistic(){
        $today = date('Y-m-d');
        $studentId = $this->request->get('studentid');
        $date = $this->request->get('date',$today);
        if($today != $date){
            $model = new AppStatisticModel();
            $list = $model->where('studentId',$studentId)->where('date',$date)->select();
            if($list->count()>0){
                $this->success('ok',$list);
            }
        }
        $requst = new HostReqest();
        $data = $requst->statistics($studentId,$date);
        if($data['status']==200){
            if(!empty($model)){
                $list = [];
                foreach ($data['data'] as $app){
                    $app['studentId'] = $studentId;
                    $app['date'] = $date;
                    $list[] = $app;
                }
                $model->insertAll($list);
            }
            
            $this->success($data['message'],$data['data']);
        }
        $this->error('err');
    }
    
    
    public function qureyAvailableTime(){
        $studentId = $this->request->get('studentid');
        $requst = new HostReqest();
        $data = $requst->queryAvailableTime($studentId);
        if($data['status']==200){
            $this->success($data['message'],$data['data']);
        }
        $this->error('err');
    }
    
    public function setAvailableTime(){
        function timeName($min){
            $timestr = '';
            if($min>0){
                $timestr = $min%60;
                $timestr = $timestr.'分钟';
                if($min>=60){
                    $timestr = intval($min/60).'小时'.$timestr;
                }
            }
            return $timestr;
        }
        $post = $this->request->post();
        $requst = new HostReqest();
        //{"studentId":"07d51f5d1577423ca1d82a570ee34fd9",
        //"weekendAvailableTime":185,
        //"dailyAvailableTime":125,
        //"type":1,
        //"dailyAvailableTimeName":"2小时5分钟",
        //"weekendAvailableTimeName":"3小时5分钟"}
        $post['type'] = 1;
        $post['dailyAvailableTimeName'] = timeName($post['dailyAvailableTime']);
        $post['weekendAvailableTimeName'] = timeName($post['weekendAvailableTime']);
        $data = $requst->setAvailableTime($post);
        if($data['status']==200){
            $this->success($data['message'],$data['data']);
        }
        $this->error('err');
    }
    
}